/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import AirLine.Airport;
import AirLine.Booking;
import AirLine.Flight;
import AirLine.Seat;
import AirLine.Airline;

import java.sql.*;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.TreeSet;
import java.util.*;

/**
 *
 * @author Fredrik
 */
public class Flights extends DBObject {

    /*
     * ORACLE_033
     */
    private static final String ORACLE_033_CODE_COLUMN = "IDNBR";
    private static final String ORACLE_033_DEPARTURE_TIME_COLUMN = "DEPARTURETIME";
    private static final String ORACLE_033_ARRIVAL_TIME_COLUMN = "ARRIVALTIME";
    private static final String ORACLE_033_PRICE_COLUMN = "FARE";
    private static final String ORACLE_033_DEPARTURE_AIRPORT_COLUMN = "DEPARTURE";
    private static final String ORACLE_033_ARRIVAL_AIRPORT_COLUMN = "DESTINATION";
    private static final String ORACLE_033_PLANE_COLUMN = "PLANE";
    private static final String ORACLE_033_FLIGHTS_TABLE_NAME = "FLIGHT";
    //For getting the seats
    private static final String ORACLE_033_FREE_SEATS_TABLE_NAME = "FREESEATS";
    private static final String ORACLE_033_SEAT_TYPE_COLUMN = "TYPE";
    private static final String ORACLE_033_SEAT_PLANEID_COLUMN = "PLANEID";
    private static final String ORACLE_033_SEAT_ROW_COLUMN = "ROWNUMBER";
    private static final String ORACLE_033_SEAT_COLUMN_COLUMN = "POSITION";
    //For booking
    private static final String ORACLE_033_BOOKING_TABLE_NAME = "BOOKING";
    private static final String ORACLE_033_BOOKING_FLIGHTID_COLUMN = "FLIGHT";
    private static final String ORACLE_033_BOOKING_FLIGH_DEPARTURE_TIME_COLUMN = "FLIGHTDEPARTURE";
    private static final String ORACLE_033_BOOKING_FLIGH_SEAT_ROW_COLUMN = "SEATROW";
    private static final String ORACLE_033_BOOKING_FLIGH_SEAT_POSITION_COLUMN = "SEATPOSITION";
    private static final String ORACLE_033_BOOKING_PLANE_ID_COLUMN = "PLANEID";
    private static final String ORACLE_033_BOOKING_PASSANGER_ID_COLUMN = "PASSANGER";
    /*
     * ORACLE_013
     */
    private static final String ORACLE_013_CODE_COLUMN = "FLIGHT_ID";
    private static final String ORACLE_013_DEPARTURE_TIME_COLUMN = "DEPARTURE_TIME";
    private static final String ORACLE_013_ARRIVAL_TIME_COLUMN = "ARRIVAL_TIME";
    private static final String ORACLE_013_PRICE_COLUMN = "FARE";
    private static final String ORACLE_013_DEPARTURE_AIRPORT_COLUMN = "FK_AIRPORT_ID_DEPARTURE";
    private static final String ORACLE_013_ARRIVAL_AIRPORT_COLUMN = "FK_AIRPORT_ID_ARRIVAL";
    private static final String ORACLE_013_PLANE_COLUMN = "FK_AIRPLANE_ID";
    private static final String ORACLE_013_FLIGHTS_TABLE_NAME = "FLIGHTVIEW";
    private static final String ORACLE_013_FLIGHT_INSTANCE_ID_COLUMN = "FLIGHTINSTANCE_ID";
    //For getting the seats
    private static final String ORACLE_013_FREE_SEATS_TABLE_NAME = "freeseats";
    private static final String ORACLE_013_SEAT_TYPE_COLUMN = "seat_TYPE";
    private static final String ORACLE_013_SEATS_FLIGHT_INSTANCE_ID_COLUMN = "flightinstance_id";
    private static final String ORACLE_013_SEAT_ROW_COLUMN = "seat_row";
    private static final String ORACLE_013_SEAT_COLUMN_COLUMN = "seat_no";
    private static final String ORACLE_013_SEAT_TYPE_WINDOW = ""+Seat.WINDOW_TYPE;
    private static final String ORACLE_013_SEAT_TYPE_ISLE = ""+Seat.ISLE_TYPE;
    private static final String ORACLE_013_SEAT_TYPE_ROW = ""+Seat.ROW_TYPE;
    //For booking
    private static final String ORACLE_013_BOOKING_TABLE_NAME = "BOOKEDTRIPS";
    private static final String ORACLE_013_BOOKING_PASSANGER_ID_COLUMN = "FK_PASSENGER_ID";
    private static final String ORACLE_013_BOOKING_FLIGHT_INSTANCE_ID_COLUMN = "FK_FLIGHT_INSTANCE_ID";
    private static final String ORACLE_013_BOOKING_FLIGH_SEAT_ID_COLUMN = "FK_SEAT_ID";
    private static final String ORACLE_013_BOOKING_FLIGH_INSTANCE_TABLE = "FLIGHTINSTANCE";
    //private static final String ORACLE_013_BOOKING_FLIGH_SEAT_ROW_COLUMN = "seat_row";
    //private static final String ORACLE_013_BOOKING_FLIGH_SEAT_POSITION_COLUMN = "seat_no";
    //private static final String ORACLE_013_BOOKING_PLANE_ID_COLUMN = "fk_airplane_id";
    /*
     * MYSQL_013
     */
    private static final String MYSQL_CODE_COLUMN = "ID";
    private static final String MYSQL_DEPARTURE_TIME_COLUMN = "departureTime";
    private static final String MYSQL_ARRIVAL_TIME_COLUMN = "arrivalTime";
    private static final String MYSQL_PRICE_COLUMN = "price";
    private static final String MYSQL_DEPARTURE_AIRPORT_COLUMN = "departure";
    private static final String MYSQL_ARRIVAL_AIRPORT_COLUMN = "destination";
    private static final String MYSQL_PLANE_COLUMN = "plane";
    private static final String MYSQL_FLIGHTS_TABLE_NAME = "Flight";
    //For getting the seats
    //private static final String MYSQL_FREE_SEATS_TABLE_NAME = "FREESEATS";
    private static final String MYSQL_SEAT_TYPE_COLUMN = "seatType";
    private static final String MYSQL_SEAT_FREE_COLUMN = "free";
    private static final String MYSQL_SEAT_PLANEID_COLUMN = "planeID";
    private static final String MYSQL_SEAT_ROW_COLUMN = "flightRow";
    private static final String MYSQL_SEAT_COLUMN_COLUMN = "flightColumn";
    //For booking
    private static final String MYSQL_BOOKING_TABLE_NAME = "Booking";
    private static final String MYSQL_BOOKING_FLIGHTID_COLUMN = "flightID";
    private static final String MYSQL_BOOKING_FLIGH_DEPARTURE_TIME_COLUMN = "departureTime";
    private static final String MYSQL_BOOKING_FLIGH_SEAT_ROW_COLUMN = "flightRow";
    private static final String MYSQL_BOOKING_FLIGH_SEAT_POSITION_COLUMN = "flightColumn";
    private static final String MYSQL_BOOKING_PLANE_ID_COLUMN = "planeID";
    private static final String MYSQL_BOOKING_PASSANGER_ID_COLUMN = "passengerSSN";
    private TreeSet<Flight> flightsList = null;
    private Hashtable<TupleKey, Flight> flights = null;
    private Airports airports = null;
    private Persons persons = null;
    private Airlines airlines = null;

    public Flights() {
        super();
    }

    public Flights(Airports airports, Airlines airlines) {
        super();
        this.airports = airports;
        this.airlines = airlines;
    }

    public void update() throws SQLException {
        connect();
        if (airports == null) {
            airports = new Airports();
            airports.update();
        }
        if (airlines == null) {
            airlines = new Airlines();
            airlines.update();
        }
        flights = new Hashtable<TupleKey, Flight>();
        flightsList = new TreeSet<Flight>();
        getAllFlightsFromOracle033();
        getAllFlightsFromOracle013();
        getAllFlightsFromMysql013();
        commit();
        closeConnections();
    }

    private void getAllFlightsFromMysql013() throws SQLException {
        if (airports == null) {
            airports = new Airports();
            airports.update();
        }

        if (airlines == null) {
            airlines = new Airlines();
            airlines.update();
        }
        if (flights == null || flightsList == null) {
            flights = new Hashtable<TupleKey, Flight>();
            flightsList = new TreeSet<Flight>();
        }
        PreparedStatement pstmnt = getConnection(MYSQL_013).prepareStatement(
                SELECT +
                MYSQL_ARRIVAL_AIRPORT_COLUMN + SEPARATOR +
                MYSQL_ARRIVAL_TIME_COLUMN + SEPARATOR +
                MYSQL_CODE_COLUMN + SEPARATOR +
                MYSQL_DEPARTURE_AIRPORT_COLUMN + SEPARATOR +
                MYSQL_DEPARTURE_TIME_COLUMN + SEPARATOR +
                MYSQL_PLANE_COLUMN + SEPARATOR +
                MYSQL_PRICE_COLUMN +
                FROM +
                MYSQL_FLIGHTS_TABLE_NAME);
        ResultSet result = pstmnt.executeQuery();
        String code, airplaneId;
        Airport from;
        Airport to;
        int price;
        Airline airline;
        Timestamp depTimestamp;
        Timestamp arrTimestamp;
        Flight flight;
        PreparedStatement getSeatsStatement;
        ResultSet seatResult;
        TreeSet<Seat> freeSeats;
        while (result.next()) {

            code = result.getString(MYSQL_CODE_COLUMN);
            from = airports.getAirportByCode(result.getString(MYSQL_DEPARTURE_AIRPORT_COLUMN));
            to = airports.getAirportByCode(result.getString(MYSQL_ARRIVAL_AIRPORT_COLUMN));
            price = result.getInt(MYSQL_PRICE_COLUMN);
            depTimestamp = result.getTimestamp(MYSQL_DEPARTURE_TIME_COLUMN);
            arrTimestamp = result.getTimestamp(MYSQL_ARRIVAL_TIME_COLUMN);
            airline = airlines.getAirlineByDatabase(MYSQL_013);
            airplaneId = result.getString(MYSQL_PLANE_COLUMN);
            freeSeats = new TreeSet<Seat>();

            getSeatsStatement = getConnection(MYSQL_013).prepareStatement(
                    "select seatType, flightColumn,planeID,flightRow from (select t.flightRow, t.flightColumn, t.planeID, t.departureTime, t.seatType,t.ID from (select s.flightRow, s.flightColumn, s.planeId, f.departureTime, s.seatType, f.ID from Seat s join Flight f on s.planeID = f.plane) t, (select flightRow, flightColumn, planeID,departureTime, flightID from Booking) b where  not (t.flightRow = b.flightRow AND t.flightColumn = b.flightColumn AND t.planeID = b.planeID AND t.departureTime = b.departureTime AND t.ID= b.flightID)) b where b.ID =? AND b.departureTime=?");
            getSeatsStatement.setString(1, code);
            getSeatsStatement.setTimestamp(2, depTimestamp);
            seatResult = getSeatsStatement.executeQuery();
            while (seatResult.next()) {
                Seat seat=new Seat(false, seatResult.getInt(MYSQL_SEAT_TYPE_COLUMN), seatResult.getString(MYSQL_SEAT_PLANEID_COLUMN), seatResult.getInt(MYSQL_SEAT_ROW_COLUMN), seatResult.getInt(MYSQL_SEAT_COLUMN_COLUMN));
                freeSeats.add(seat);
                //System.out.println("MYSQL 33 seat: "+seat.getType());
            }
            getSeatsStatement.close();
            seatResult.close();

            //Dont care about full planes
            if (freeSeats.size() > 0) {

                flight = new Flight(code, from, to, price, depTimestamp, arrTimestamp, freeSeats, MYSQL_013, airline, airplaneId);
                flights.put(new TupleKey(code, depTimestamp), flight);
                flightsList.add(flight);
            }

        }
        result.close();
        pstmnt.close();
    }

    private void getAllFlightsFromOracle033() throws SQLException {
        if (airports == null) {
            airports = new Airports();
            airports.update();
        }
        if (airlines == null) {
            airlines = new Airlines();
            airlines.update();
        }

        if (flights == null || flightsList == null) {
            flights = new Hashtable<TupleKey, Flight>();
            flightsList = new TreeSet<Flight>();
        }
        PreparedStatement pstmnt = getConnection(ORACLE_033).prepareStatement(
                SELECT +
                ORACLE_033_ARRIVAL_AIRPORT_COLUMN + SEPARATOR +
                ORACLE_033_ARRIVAL_TIME_COLUMN + SEPARATOR +
                ORACLE_033_CODE_COLUMN + SEPARATOR +
                ORACLE_033_DEPARTURE_AIRPORT_COLUMN + SEPARATOR +
                ORACLE_033_DEPARTURE_TIME_COLUMN + SEPARATOR +
                ORACLE_033_PLANE_COLUMN + SEPARATOR +
                ORACLE_033_PRICE_COLUMN +
                FROM +
                ORACLE_033_FLIGHTS_TABLE_NAME);
        ResultSet result = pstmnt.executeQuery();
        String code, airplaneId;
        Airport from;
        Airport to;
        Airline airline;
        int price;
        Timestamp depTimestamp;
        Timestamp arrTimestamp;
        Flight flight;
        PreparedStatement getSeatsStatement;
        ResultSet seatResult;
        TreeSet<Seat> freeSeats;
        while (result.next()) {

            code = result.getString(ORACLE_033_CODE_COLUMN);
            from = airports.getAirportByCode(result.getString(ORACLE_033_DEPARTURE_AIRPORT_COLUMN));
            to = airports.getAirportByCode(result.getString(ORACLE_033_ARRIVAL_AIRPORT_COLUMN));
            price = result.getInt(ORACLE_033_PRICE_COLUMN);
            depTimestamp = result.getTimestamp(ORACLE_033_DEPARTURE_TIME_COLUMN);
            arrTimestamp = result.getTimestamp(ORACLE_033_ARRIVAL_TIME_COLUMN);
            airline = airlines.getAirlineByDatabase(ORACLE_033);
            airplaneId = result.getString(ORACLE_033_PLANE_COLUMN);

            freeSeats = new TreeSet<Seat>();
            getSeatsStatement = getConnection(ORACLE_033).prepareStatement(
                    SELECT +
                    ORACLE_033_SEAT_TYPE_COLUMN + SEPARATOR +
                    ORACLE_033_SEAT_COLUMN_COLUMN + SEPARATOR +
                    ORACLE_033_SEAT_PLANEID_COLUMN + SEPARATOR +
                    ORACLE_033_SEAT_ROW_COLUMN + FROM +
                    ORACLE_033_FREE_SEATS_TABLE_NAME + " WHERE FLIGHT=? AND FLIGHTDEPARTURE=?");
            getSeatsStatement.setString(1, result.getString(ORACLE_033_CODE_COLUMN));
            getSeatsStatement.setTimestamp(2, depTimestamp);
            seatResult = getSeatsStatement.executeQuery();
            while (seatResult.next()) {
                Seat seat=new Seat(false, Integer.valueOf(seatResult.getString(ORACLE_033_SEAT_TYPE_COLUMN)), seatResult.getString(ORACLE_033_SEAT_PLANEID_COLUMN), seatResult.getInt(ORACLE_033_SEAT_ROW_COLUMN), seatResult.getInt(ORACLE_033_SEAT_COLUMN_COLUMN));
                //System.out.println("inlagt : "+seat.getType()+"|"+seat.getRow()+"|"+seat.getColumn()+seat.getPlaneid());
                //System.out.println(freeSeats.add(seat));
                
                //System.out.println(seat.getRow());
                
                 //System.out.println("new flightseats size: ");
                 
                for(Seat s: freeSeats)
                {
                   // System.out.println(s.getType());
                }
            }
            
            getSeatsStatement.close();
            seatResult.close();

            //Dont care about full planes
            if (freeSeats.size() > 0) {

               
                
                
                flight = new Flight(code, from, to, price, depTimestamp, arrTimestamp, freeSeats, ORACLE_033, airline, airplaneId);
                flights.put(new TupleKey(code, depTimestamp), flight);
                flightsList.add(flight);
            }

        }
        result.close();
        pstmnt.close();
    }

    private void getAllFlightsFromOracle013() throws SQLException {
        if (airports == null) {
            airports = new Airports();
            airports.update();
        }

        if (airlines == null) {
            airlines = new Airlines();
            airlines.update();
        }

        if (flights == null || flightsList == null) {
            flights = new Hashtable<TupleKey, Flight>();
            flightsList = new TreeSet<Flight>();
        }
        String sql = SELECT +
                ORACLE_013_CODE_COLUMN + SEPARATOR +
                ORACLE_013_DEPARTURE_TIME_COLUMN + SEPARATOR +
                ORACLE_013_ARRIVAL_TIME_COLUMN + SEPARATOR +
                ORACLE_013_PRICE_COLUMN + SEPARATOR +
                ORACLE_013_DEPARTURE_AIRPORT_COLUMN + SEPARATOR +
                ORACLE_013_ARRIVAL_AIRPORT_COLUMN + SEPARATOR +
                ORACLE_013_PLANE_COLUMN + SEPARATOR +
                ORACLE_013_FLIGHT_INSTANCE_ID_COLUMN +
                FROM +
                ORACLE_013_FLIGHTS_TABLE_NAME;
        PreparedStatement pstmnt = getConnection(ORACLE_013).prepareStatement(sql);
        ResultSet result = pstmnt.executeQuery();
        String code, airplaneId;
        Airport from;
        Airport to;
        int price;
        Timestamp depTimestamp;
        Timestamp arrTimestamp;
        Flight flight;
        PreparedStatement getSeatsStatement;
        ResultSet seatResult;
        TreeSet<Seat> freeSeats;
        Airline airline;
        while (result.next()) {

            code = result.getString(ORACLE_013_CODE_COLUMN);
            from = airports.getAirportByCode(result.getString(ORACLE_013_DEPARTURE_AIRPORT_COLUMN));
            to = airports.getAirportByCode(result.getString(ORACLE_013_ARRIVAL_AIRPORT_COLUMN));
            price = result.getInt(ORACLE_013_PRICE_COLUMN);
            depTimestamp = result.getTimestamp(ORACLE_013_DEPARTURE_TIME_COLUMN);
            arrTimestamp = result.getTimestamp(ORACLE_013_ARRIVAL_TIME_COLUMN);
            airplaneId = result.getString(ORACLE_013_PLANE_COLUMN);
            airline = airlines.getAirlineByDatabase(ORACLE_013);
            freeSeats = new TreeSet<Seat>();
            getSeatsStatement = getConnection(ORACLE_013).prepareStatement(
                    SELECT +
                    ORACLE_013_SEAT_TYPE_COLUMN + SEPARATOR +
                    ORACLE_013_SEAT_COLUMN_COLUMN + SEPARATOR +
                    ORACLE_013_SEATS_FLIGHT_INSTANCE_ID_COLUMN + SEPARATOR +
                    ORACLE_013_SEAT_ROW_COLUMN +
                    FROM +
                    ORACLE_013_FREE_SEATS_TABLE_NAME +
                    " WHERE " +
                    ORACLE_013_SEATS_FLIGHT_INSTANCE_ID_COLUMN +
                    "=? ");
            getSeatsStatement.setString(1, result.getString(ORACLE_013_FLIGHT_INSTANCE_ID_COLUMN));
            seatResult = getSeatsStatement.executeQuery();
           
            while (seatResult.next()) {
                int seatType;
                if ((seatResult.getString(ORACLE_013_SEAT_TYPE_COLUMN)).equals(ORACLE_013_SEAT_TYPE_WINDOW)) {
                    seatType = Seat.WINDOW_TYPE;
                } else if ((seatResult.getString(ORACLE_013_SEAT_TYPE_COLUMN)).equals(ORACLE_013_SEAT_TYPE_ISLE)) {
                    seatType = Seat.ISLE_TYPE;
                } else {
                    seatType = Seat.ROW_TYPE;
                }
                Seat newSeat = new Seat(false, seatType, seatResult.getString(ORACLE_013_SEATS_FLIGHT_INSTANCE_ID_COLUMN), seatResult.getInt(ORACLE_013_SEAT_ROW_COLUMN), seatResult.getInt(ORACLE_013_SEAT_COLUMN_COLUMN));
                
                
                freeSeats.add(newSeat);
            }
            getSeatsStatement.close();
            seatResult.close();

            //Dont care about full planes
            if (freeSeats.size() > 0) {

                flight = new Flight(code, from, to, price, depTimestamp, arrTimestamp, freeSeats, ORACLE_013, airline, airplaneId);
                flights.put(new TupleKey(code, depTimestamp), flight);
                flightsList.add(flight);
            }

        }
        result.close();
        pstmnt.close();

    }

    public void populate() throws SQLException {
        if (flights == null || flightsList == null) {
            update();
        }
    }

    public Hashtable<TupleKey, Flight> getFlights() throws SQLException {
        if (flights == null) {
            populate();
        }
        return flights;
    }

    public TreeSet<Flight> getFlightsList() throws SQLException {
        if (flightsList == null) {
            populate();
        }
        return flightsList;
    }

    public Flight getFlightByKey(String code, Timestamp deptime) throws SQLException {
        return getFlights().get(new TupleKey(code, deptime));
    }

    public void book(Booking b) throws UnableToBookException, SQLException {

        if (persons == null) {
            persons = new Persons();
        }
        persons.registerPerson(b.getPerson(), b.getFlight().getAirline().getDbIdentifier());

        PreparedStatement pstmnt = null;

        if (b.getFlight().getAirline().getDbIdentifier().equals(ORACLE_033)) {
            String myQuerry = INSERT_INTO +
                    ORACLE_033_BOOKING_TABLE_NAME +
                    " (" + ORACLE_033_BOOKING_FLIGHTID_COLUMN + SEPARATOR +
                    ORACLE_033_BOOKING_FLIGH_DEPARTURE_TIME_COLUMN + SEPARATOR +
                    ORACLE_033_BOOKING_FLIGH_SEAT_POSITION_COLUMN + SEPARATOR +
                    ORACLE_033_BOOKING_FLIGH_SEAT_ROW_COLUMN + SEPARATOR +
                    ORACLE_033_BOOKING_PASSANGER_ID_COLUMN + SEPARATOR +
                    ORACLE_033_BOOKING_PLANE_ID_COLUMN +
                    ") VALUES(?,?,?,?,?,?)";
            pstmnt = getConnection(b.getFlight().getAirline().getDbIdentifier()).prepareStatement(myQuerry);
            pstmnt.setString(1, b.getFlight().getCode());
            pstmnt.setTimestamp(2, b.getFlight().getDepartureTime());
            pstmnt.setString(3, String.valueOf(b.getSeat().getColumn()));
            pstmnt.setInt(4, b.getSeat().getRow());
            pstmnt.setString(5, b.getPerson().getSSN());
            pstmnt.setString(6, b.getFlight().getPlaneId());
        } else if (b.getFlight().getAirline().getDbIdentifier().equals(ORACLE_013)) {

            String sqlseatid = "select seat_id from (select * from seat natural join FLIGHTINSTANCE natural join flight) a where a.seat_row = ? AND a.FLIGHTINSTANCE_ID = ? AND a.seat_no = ?";
            PreparedStatement pstmnt2 = getConnection(ORACLE_013).prepareStatement(sqlseatid);
            pstmnt2.setInt(1, b.getSeat().getRow());
            pstmnt2.setInt(3, b.getSeat().getColumn());
            pstmnt2.setString(2, b.getSeat().getPlaneid());
            ResultSet result2 = pstmnt2.executeQuery();
            result2.next();
//            System.out.println(b.getSeat().getRow()+ "  " +  b.getSeat().getColumn() + "   " +   b.getSeat().getPlaneid() );
//            System.out.println(result2.getString("seat_id"));           
            

            String myQuerry = INSERT_INTO +
                    ORACLE_013_BOOKING_TABLE_NAME +
                    " (" + ORACLE_013_BOOKING_FLIGHT_INSTANCE_ID_COLUMN + SEPARATOR +
                    ORACLE_013_BOOKING_PASSANGER_ID_COLUMN + SEPARATOR +
                    ORACLE_013_BOOKING_FLIGH_SEAT_ID_COLUMN +
                    ") VALUES(?,?,?)";
            pstmnt = getConnection(b.getFlight().getAirline().getDbIdentifier()).prepareStatement(myQuerry);
            pstmnt.setString(1, b.getSeat().getPlaneid());
            pstmnt.setString(2, b.getPerson().getSSN());
            pstmnt.setString(3, result2.getString("seat_id"));

//            pstmnt2.close();
//            result2.close();
//            pstmnt3.close();
//            result3.close();


        } else if (b.getFlight().getAirline().getDbIdentifier().equals(MYSQL_013)) {
            if (persons == null) {
                persons = new Persons();
            }
            persons.registerPerson(b.getPerson(), b.getFlight().getAirline().getDbIdentifier());

            pstmnt = null;
            String myQuerry = INSERT_INTO +
                    MYSQL_BOOKING_TABLE_NAME +
                    " (" + MYSQL_BOOKING_FLIGHTID_COLUMN + SEPARATOR +
                    MYSQL_BOOKING_FLIGH_DEPARTURE_TIME_COLUMN + SEPARATOR +
                    MYSQL_BOOKING_FLIGH_SEAT_POSITION_COLUMN + SEPARATOR +
                    MYSQL_BOOKING_FLIGH_SEAT_ROW_COLUMN + SEPARATOR +
                    MYSQL_BOOKING_PASSANGER_ID_COLUMN + SEPARATOR +
                    MYSQL_BOOKING_PLANE_ID_COLUMN +
                    ") VALUES(?,?,?,?,?,?)";
            pstmnt = getConnection(b.getFlight().getAirline().getDbIdentifier()).prepareStatement(myQuerry);
            pstmnt.setString(1, b.getFlight().getCode());
            pstmnt.setTimestamp(2, b.getFlight().getDepartureTime());
            pstmnt.setString(3, String.valueOf(b.getSeat().getColumn()));
            pstmnt.setInt(4, b.getSeat().getRow());
            pstmnt.setString(5, b.getPerson().getSSN());
            pstmnt.setString(6, b.getFlight().getPlaneId());
        }
        pstmnt.execute();


    }
    }
